/*
 * DateMan.java
 * Created on 06. Mai 2003, 09:10
 * by chrmac@gmx.de
 * --------
 * This Class makes all date claculations, UNIX<->String
 * 
 */

package info.chrm.helper;

/**
* This Class represents the date manger.
*/
public class DateMan {
	java.util.Date myDate;		
	long unixTime = 0;	// Unix time in ms
	String yearS = ("0000");
	String monthS = ("00");
	String dayS = ("00");
	String hourS = ("00");
	String minS = ("00");
	String secS = ("00");
	
	int day=0, month=0, year=0;
	int sec=0, min=0, hour=0;
	
	/** Default C'tor.
	 *  Sets the time to current.
	 */
	public DateMan () {
		this.setTime(-1);			
	}
	
	/** This C'tor allows You to set a specific time-stamp in seconds.
	 */
	public DateMan (long _unixTime) {
		this.setTime(_unixTime);			
	}

	/** Sets the time of this object.
	 *  The param is a Unix-timestamp in seconds
	 */
	public void setTime(long _unixTime) {
		this.unixTime = _unixTime * 1000;
		
		if(this.unixTime >= 0) {
			myDate = new java.util.Date(this.unixTime);
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(myDate);
			this.year = cal.get(cal.YEAR);
			this.month = cal.get(cal.MONTH) + 1;
			this.day = cal.get(cal.DAY_OF_MONTH);
			this.hour = cal.get(cal.HOUR_OF_DAY);
			this.min = cal.get(cal.MINUTE);
			this.sec = cal.get(cal.SECOND);

			yearS = Integer.toString(this.year);
		
			if(this.month >= 10) monthS = Integer.toString(this.month);
			else monthS = "0" + Integer.toString(this.month);
		
			if(this.day >= 10) dayS = Integer.toString(this.day);
			else dayS = "0" + Integer.toString(this.day);	
			
			if(this.hour >= 10) hourS = Integer.toString(this.hour);
			else hourS = "0" + Integer.toString(this.hour);
		
			if(this.min >= 10) minS = Integer.toString(this.min);
			else minS = "0" + Integer.toString(this.min);
		
			if(this.sec >= 10) secS = Integer.toString(this.sec);
			else secS = "0" + Integer.toString(this.sec);
		}
		else {
			myDate = new java.util.Date();
			java.util.Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(myDate);
			this.unixTime = cal.getTimeInMillis();			
			this.setTime(this.unixTime / 1000);
		}
	}
	
	/** Sets the time of this object.
	 * @param _year The Year (eg.: "1999")
	 * @param _month The Month, begins with 0 (eg.: "2" := March)
	 * @param _date The day (eg.: "12")
	 * @param _hour The Hour (eg.: "23")
	 * @param _minute The Minute (eg.: "59")
	 * @param _second The Second (eg.: "1")
	 * */
	public void setTime(int _year, int _month, int _date, int _hour, int _minute, int _second) {
		 java.util.Calendar cal = java.util.Calendar.getInstance();
		 cal.set(_year, _month, _date, _hour, _minute, _second);

		 this.year = cal.get(cal.YEAR);
		 this.month = cal.get(cal.MONTH) + 1;
		 this.day = cal.get(cal.DAY_OF_MONTH);
		 this.hour = cal.get(cal.HOUR_OF_DAY);
		 this.min = cal.get(cal.MINUTE);
		 this.sec = cal.get(cal.SECOND);
		 
		 yearS = Integer.toString(this.year);
	  
		 if(this.month >= 10) monthS = Integer.toString(this.month);
		 else monthS = "0" + Integer.toString(this.month);
	  
		 if(this.day >= 10) dayS = Integer.toString(this.day);
		 else dayS = "0" + Integer.toString(this.day);	
		  
		 if(this.hour >= 10) hourS = Integer.toString(this.hour);
		 else hourS = "0" + Integer.toString(this.hour);
	  
		 if(this.min >= 10) minS = Integer.toString(this.min);
		 else minS = "0" + Integer.toString(this.min);
	  
		 if(this.sec >= 10) secS = Integer.toString(this.sec);
		 else secS = "0" + Integer.toString(this.sec);

		 this.unixTime = cal.getTimeInMillis();
	}

	/** Sets objects time _offset seconds earlier or later
	 * */
	public void offsetTime(long _offset) {
		this.setTime((long)(this.unixTime/1000) + _offset);		
	}
	
	/** Gets the time as formatted String.
	 *  for example:
	 *  getTime(0) returns "2003-05-05 09:38:33"
	 */
	public String getTime(int _style) {
		if(this.unixTime < 0) return "undef";
		
		if(_style == 0) return 
			(yearS + "-" + monthS + "-" + dayS + " " + hourS + ":" + minS + ":" + secS);
		
		if(_style == 1) return 
			(yearS + "-" + monthS + "-" + dayS);
		
		if(_style == 2) return 
			(hourS + ":" + minS + ":" + secS);
		
		if(_style == 3) return 
			(hourS + ":" + minS);
		
		if(_style == 4) return 
			(minS + ":" + secS);

		if(_style == 5) return 
			(monthS + "-" + dayS + " " + hourS + ":" + minS);
		
		return "none";
	}
	
	/** Gets the UNIX-TimeStamp.
	 * @param _style 0: Seconds, 1: Milis
	 */
	public long getTimeStamp(int _style) {
		if(_style == 0)
			return (long)(this.unixTime / 1000);
		else
			return this.unixTime;
	}
}
